const fs = require('fs');

// var globalData = '';
var cnt = 0;

var startTime = new Date();

var rds = fs.createReadStream('input.txt');
var wrs = fs.createWriteStream('data.txt');

rds.on('data', function(trunk){
    cnt ++;
    console.log('第' + cnt + '次读取，共' + Buffer.byteLength(trunk) + '字节。');
    // globalData += trunk;

    wrs.write(trunk);

    // console.log(trunk);
});

rds.on('end', function(){
    console.log("读取文件已完成！");
    // wrs.write(globalData);
});

rds.on('close', function(){
    console.log('读文件已关闭！')
    // console.log(globalData);

    wrs.end();
});

rds.on('open', function(){
    console.log("读文件已打开！");
});

rds.on('error', function(err){
    console.log(err);
});

wrs.on('finish', function(){
    console.log('写入完成！');
    var endTime = new Date();
    console.log('总耗时：' + (endTime - startTime) + '毫秒。')
});

wrs.on('error', function(err){
    console.log(err);
});

wrs.on('close', function(){
    console.log('写文件已关闭！');
});